Categories
JavaScript

Good Parts of JavaScript — Flow Control

Spread the love

JavaScript is one of the most popular programming languages in the world. It can do a lot and have some features that are ahead of many other languages.

In this article, we’ll look at some good parts of JavaScript, including flow control.

Falsy Values

Falsy values in JavaScript will prevent an if or else if block from running.

Falsy values are the following:

  • false
  • null
  • undefined
  • empty string ‘’
  • 0
  • NaN

Everything else is truthy.

Switch Statements

switch statements perform multiway branching.

It compares an expression with the specified cases.

A case can be a statement or a block. A case statement or block can contain one or more expressions.

The case expressions don’t have to be constants.

While Statement

A while statement performs a simple loop. The loop will stop running if the condition that’s after while is falsy.

Otherwise, the loop body will run.

For Statement

A for statement is more complex than a while loop.

It may contain 3 optional clauses, which is the initialization code, the condition, while the loop runs, and the increment statement.

First, the initialization code is run.

Then the condition is evaluated at each iteration. If the condition is truthy, then the loop will run.

Otherwise, the loop will break.

Then the increment statement runs. The statement updates the index variable.

The for loop looks like:

for (let i = 0; i < MAX; i++) {  
  //...  
}

The loop runs from i is 0 from i is one less than MAX .

For-In Loop

The for-in loop loops through the keys of an object, including any inherited properties.

The order that the keys are looped depends on how the browser implements the for-in loop.

This means that we can’t depend on the order that the keys are being looped through.

To check if the property is inherited or not, we can use the hasOwnProperty method that comes with objects.

We can do that by writing the following code:

for (const key in obj) {  
  if (obj.hasOwnProperty(key)) {  
    //..  
  }  
}

In the code above, we have the key that we looped through in obj , including inherited properties.

Therefore, we’ve to call obj.hasOwnProperty(key) if we only want to deal with keys that are in obj itself.

For-Of Loop

The for-of loop loops through the items that are in any iterable object.

This includes things like arrays, NodeLists, sets, maps, generators, and anything else we create.

For instance, we can write:

for (const a in arr) {  
  //..  
}

This means looping through items easier than with a while or for loop since we don’t have to specify any conditions.

It just loops through all the entries that are in the iterable object.

Try Statement

The try statement runs a block of code and catches an exception that was thrown in the block.

The catch clauses go after the try clause and receive the exception object.

It looks like:

try {  
  //...  
} catch (ex) {  
  //...  
}

Throw Statement

The throw statement is used to throw exceptions, which can be caught with the try...catch block.

For instance, we can write:

throw new Error('error');

We can throw anything with the throw statement, but the error object has information like stack traces and line number of where the error occurred that aren’t available anywhere else.

Return Statement

The return statement lets us end a function wherever we want inside the function block.

JavaScript doesn’t allow a line between the return and expression that it’s returning.

Break Statement

break statements are for ending loops or terminating the switch statement.

We can also add a label to end a labeled statement.

break and the label has to be the same line.

Expressions Statement

An expression statement can either assign variables or one or more variables or members.

It can also run a method or delete property from an object.

The = operator is used for assignment, which we shouldn’t confuse with the === operator.

The += operator can add or concatenate.

Conclusion

break statements are used for ending loops and switch cases.

return is used for ending functions anywhere in our code.

The throw statement is used to throw any object as an error which can then be caught by the catch block.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *